﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace WebApi2017.Controllers
{

    //[CrossSite]
    public class ApiDemoController : ApiController
    {
        public async Task<IHttpActionResult> GetTest(int id, string a)
        {
            IHttpActionResult HttpActionResult = this.a(id);
            HttpResponseMessage HttpRequestMessage = HttpActionResult.ExecuteAsync(CancellationToken.None).Result;
            if (!HttpRequestMessage.IsSuccessStatusCode)
            {
                return HttpActionResult;
            }
            HttpActionResult = await b(id);
            if (!HttpRequestMessage.IsSuccessStatusCode)
            {
                return HttpActionResult;
            }
            HttpActionResult = await c(id);
            if (!HttpRequestMessage.IsSuccessStatusCode)
            {
                return HttpActionResult;
            }
            return HttpActionResult;
        }
        public IHttpActionResult a(int id)
        {
            if (id > 1)
            {
                return Ok("BadRequest" + id.ToString());
            }
            else
            {
                return BadRequest("BadRequest" + id.ToString());
            }
        }
        public async Task<IHttpActionResult> b(int id)
        {
            return await Task.Run(() =>
            {
                return BadRequest("BadRequest" + id.ToString());
            });
        }
        public async Task<IHttpActionResult> c(int id)
        {
            return await Task.Run(() =>
            {
                return Ok(id);
            });
        }

        // GET 访问路径 {域名}/api/ApiDemo/Get 用于查询       
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value11" };
        }

        // GET 访问路径 {域名}/api/ApiDemo/Get/1 用于查询
        public string Get(int id)
        {
            return "value1";
        }
        public string Get3(int id,string a,string b,string c)
        {
            return id+ "Get3" + a+b+c;
        }
        public class abc1
        {
            public int id { get; set; }
            public string a { get; set; }
            public string b { get; set; }
            public string c { get; set; }
            public List<abc_sub> abc_sub { get; set; }
        }
        public class abc_sub
        {
            public string type { get; set; }
            public string value { get; set; }
        }
        public string Post3(abc1 abc1)
        {
            string str = abc1.id + "post3" + abc1.a + abc1.b + abc1.c;
            if (abc1.abc_sub != null)
            {
                str += "TYPE=" + abc1.abc_sub[0].type + abc1.abc_sub[0].value;
            }

            return str;
        }        

        // Put访问路径 {域名}/api/ApiDemo/Put {参数实体类,要有一个id参数} 用于修改
        public void Put(int id, [FromBody]string value)
        {
        }

        // Put访问路径 {域名}/api/ApiDemo/Delete/1 用于删除
        public void Delete(int id)
        {
        }
    }
}